package com.gtq.uhome.controller;


import com.gtq.uhome.result.Result;
import com.gtq.uhome.service.AdministratorLoginService;
import com.gtq.uhome.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 站点 用户登录界面
 */
@Controller
@RequestMapping(value = "/sysUser")
@ResponseBody
@CrossOrigin
@Api(tags = "站点/配送员模块 用户登录接口说明文档 咨询李龙")
public class AdministratorLoginController {

    @Resource
    AdministratorLoginService administratorLoginService;

    @PostMapping("/login")
    @ApiOperation(value = "站点登录", notes = "先咨询后调试，需要传送staffNumber：用户账户，staffPassword：用户密码,测试登录账号和密码已被设为默认值，直接测试即可", response = Map.class, httpMethod = "POST")
    @ApiImplicitParam(name = "map", value = "接收的容器", defaultValue = "{\"staffNumber\":\"123456789\",\"staffPassword\":\"123456789\"}", dataType = "{\"staffNumber\":\"123456789\",\"staffPassword\":\"123456789\"}", required = true, paramType = "body")
    public Result administratorLogin(@RequestBody Map map) {
        String staffNumber = (String) map.get("staffNumber");
        String staffPassword = (String) map.get("staffPassword");
        System.out.println("用户的帐户:" + staffNumber + "用户的密码:" + staffPassword);
        Map<String, Object> userPowerVO = administratorLoginService.administratorLogin(staffNumber, staffPassword);
        if (userPowerVO != null) {
            //使用JWT工具类
            JWTUtil jwtUtil = new JWTUtil();
            String Id = userPowerVO.get("userId").toString();
            //subject 使用json
            String subject="{\"alg\":\"HS256\",\"typ\":\"JWT\"}";
            //创建JWT
            String jwt = jwtUtil.createJWT(Id, subject, 60 * 60*1000, userPowerVO);
            //
            userPowerVO.put("Authorization", jwt);
            System.out.println("令牌："+jwt);
            return Result.ok(userPowerVO, "登录成功");
        } else{
            return Result.failed("登录失败，请检查用户账号和密码");
        }
    }


    @PostMapping("/update")
    @ApiOperation(value = "站点修改密码", notes = "先咨询后调试，需要传送staffNumber：用户账户，staffPassword：用户密码,newStaffPassword：用户新密码。", response = Map.class, httpMethod = "POST")
    @ApiImplicitParam(name = "map", value = "接收的容器", defaultValue = "null", dataType = "String", required = true, paramType = "body")
    public Result updateAdministratorPassword(@RequestBody Map map) {
        String staffNumber = (String) map.get("staffNumber");
        String staffPassword = (String) map.get("staffPassword");
        String newStaffPassword = (String) map.get("newStaffPassword");

        System.out.println("用户的帐户:" + staffNumber + "用户的密码:" + staffPassword + "用户新密码:" + newStaffPassword);
        Boolean aBoolean = administratorLoginService.updateAdministratorPassword(staffNumber, staffPassword, newStaffPassword);
        if (aBoolean)
            return Result.ok(null, "修改成功");
        else
            return Result.failed("修改失败，请检查用户账号和密码");
    }

    @PostMapping("/updatePassword")
    @ApiOperation(value = "修改配送员密码", notes = "先咨询后调试，需要传送staffId：配送员登录成功返回的id，staffPassword：配送员原密码,newStaffPassword：配送员新密码。", response = Map.class, httpMethod = "POST")
    @ApiImplicitParam(name = "map", value = "接收的容器", defaultValue = "null", dataType = "String", required = true, paramType = "body")
    public Result updateCourierAdministratorPassword(@RequestBody Map map) {
        String strStaffId = (String) map.get("staffId");
        String staffPassword = (String) map.get("staffPassword");
        String newStaffPassword = (String) map.get("newStaffPassword");
        Integer staffId = Integer.parseInt(strStaffId);
        System.out.println("用户的帐户:" + staffId + "用户的密码:" + staffPassword + "用户新密码:" + newStaffPassword);
        Boolean aBoolean = administratorLoginService.updateCourierAdministratorPassword(staffId,staffPassword,newStaffPassword);
        if (aBoolean)
            return Result.ok(null, "修改成功");
        else
            return Result.failed("修改失败，请检查用户账号和密码");
    }

    @GetMapping("/exit")
    @ApiOperation(value = "站点用户退出", notes = "消除Authorization请求头", response = Map.class, httpMethod = "GET")
    public Result exit(HttpServletRequest request) {
        String header = request.getHeader("Authorization");
        //获取seseion，没有就返回空
        HttpSession session = request.getSession(false);
        //进行判断
        if (session!=null){
            session.removeAttribute("Authorization");
            return Result.ok(null,"退出成功");
        }
        return Result.failed("退出失败");
    }

}
